package com.library.web.controller.library;


import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.TableDataInfo;
import com.library.common.enums.BusinessType;
import com.library.common.utils.file.FileUploadUtils;
import com.library.common.utils.poi.ExcelUtil;
import com.library.domain.MbGoods;
import com.library.framework.config.CosConfig;
import com.library.framework.manager.BizCodeManager;
import com.library.framework.manager.CosUploadManager;
import com.library.service.IMbGoodsService;
import com.library.service.IMbGoodsTypeService;
import com.library.vo.SelectBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 积分商城 信息操作处理
 *
 * @author null
 * @date 2020-04-23
 * @blame Android Team
 */
@RestController
@RequestMapping("/library/mbGoods")
@SuppressWarnings("all")
public class MbGoodsController extends BaseController {
    private String cosPrefix = "/goods/";
    @Autowired
    private IMbGoodsService mbGoodsService;
    @Autowired
    private IMbGoodsTypeService mbGoodsTypeService;
    @Autowired
    private BizCodeManager bizCodeManager;
    @Autowired
    private CosConfig cosConfig;

    /**
     * 根据ID查询商品
     *
     * @param goods 商品对象
     * @return
     */
    @RequestMapping("/selectGoodById")
    public AjaxResult selectGoodById(MbGoods goods) {
        AjaxResult ajaxResult = new AjaxResult();
        MbGoods mbGoods = mbGoodsService.selectMbGoodsById(goods.getId().longValue());
        if (mbGoods == null) {
            return Objects.requireNonNull(ajaxResult.put("data", "商品[" + mbGoods.getGoodsName() + "]不存在"))
                    .put("code", 500);
        }
        return ajaxResult.put("data", mbGoods).put("code", 200);
    }


    /**
     * 商品下拉模糊查询
     *
     * @param request 请求对象
     * @return
     */
    @GetMapping("/selectGoodName")
    @ResponseBody
    public List<SelectBean> selectGoodName(HttpServletRequest request) {
        String name = request.getParameter("search");
        MbGoods goods = new MbGoods();
        goods.setGoodsName(name);
        List<MbGoods> list = mbGoodsService.selectMbGoodsList(goods);
        List<SelectBean> selectBeans = new ArrayList<>();
        list.forEach(mbGoods -> {
            SelectBean selectBean = new SelectBean();
            selectBean.setId(mbGoods.getId().toString());
            selectBean.setText(mbGoods.getGoodsName().concat("(").concat(mbGoods.getGoodsNo())
                    .concat(")"));
            selectBeans.add(selectBean);
        });
        return selectBeans;
    }


    /**
     * 查询积分商城列表
     */
    @PreAuthorize("@ss.hasPermi('library:mbGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(MbGoods mbGoods) {
        startPage();
        List<MbGoods> list = mbGoodsService.selectMbGoodsList(mbGoods);
        return getDataTable(list);
    }


    /**
     * 导出积分商城列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MbGoods mbGoods) {
        List<MbGoods> list = mbGoodsService.selectMbGoodsList(mbGoods);
        ExcelUtil<MbGoods> util = new ExcelUtil<MbGoods>(MbGoods.class);
        return util.exportExcel(list, "mbGoods");
    }


    /**
     * 获取积分商城详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(mbGoodsService.selectMbGoodsById(id));
    }

    /**
     * 新增积分商城
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "积分商城", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MbGoods mbGoods) {
        mbGoods.setGoodsNo(bizCodeManager.createSp());
        return toAjax(mbGoodsService.insertMbGoods(mbGoods));
    }

    /**
     * 修改积分商城
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "积分商城", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MbGoods mbGoods) {
        return toAjax(mbGoodsService.updateMbGoods(mbGoods));
    }

    /**
     * 删除积分商城
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "积分商城", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mbGoodsService.deleteMbGoodsByIds(ids));
    }

}
